using Cinemachine;
using MetalMax;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;
namespace Adventure
{
    public class ControllerForm : UGuiForm
    {
        private UIJoystick uiJoystick;
        private InputComponent input;

        private UIButton jumpButton;
        private UIButton attackButton;
        private UIButton backButton;

        private ProcedureMain procedureMain;

        private Text GoldText, KillCountText, EXPText;

        private int goldCount, killCount, expCount;
        private void Awake()
        {
            uiJoystick = transform.FindTransform<UIJoystick>("UIJoystick");

            input = GameEntry.InputComponent;
            jumpButton = transform.FindTransform<UIButton>("JumpButton");
            attackButton = transform.FindTransform<UIButton>("AttackButton");
            backButton = transform.FindTransform<UIButton>("BackButton");

            GoldText = transform.FindTransform<Text>("GoldText");
            KillCountText = transform.FindTransform<Text>("KillCountText");
            EXPText = transform.FindTransform<Text>("EXPText");
        }

        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);
            if (userData != null)
            {

                procedureMain = (ProcedureMain)userData;
            }

            
            goldCount =0;
            killCount = 0;
            expCount = 0;
            GetGoldUpdate(0);
            GetKillUpdate(0);
            GetEXPUpdate(0);

            GameEntry.DataNode.SetData<VarInt32>(Constant.ControllerFormSerialId, GetComponent<UIForm>().SerialId);
        }
        protected override void OnClose(bool isShutdown, object userData)
        {
            base.OnClose(isShutdown, userData);

            Camera.main.GetComponent<CinemachineVirtualCamera>().Follow = null;
            Camera.main.GetComponent<CinemachineVirtualCamera>().LookAt = null;
            Camera.main.transform.position = new Vector3(0, 0, -10);

        }
        private void OnEnable()
        {
            UIEventTrigger.Get(jumpButton).onClick = OnJumpClick;
            UIEventTrigger.Get(attackButton).onClick = OnAttackClick;
            UIEventTrigger.Get(backButton).onClick = OnBackClick;         
        }
        private void Update()
        {
            if (Input.GetAxisRaw("Horizontal") == 0)
                input.AndroidInput(InputComponent.AndroidKeyType.Movement, uiJoystick.Horizontal);
        }

        private void OnDisable()
        {
            UIEventTrigger.Get(jumpButton).onClick = null;
            UIEventTrigger.Get(attackButton).onClick = null;
            UIEventTrigger.Get(backButton).onClick = null;
        }
        private void OnAttackClick(GameObject go)
        {
            input.AndroidInput(InputComponent.AndroidKeyType.Attack);
          
        }

        private void OnJumpClick(GameObject go)
        {
            input.AndroidInput(InputComponent.AndroidKeyType.Jump);
        }
        private void OnBackClick(GameObject go)
        {
            
            PlayUISound(10000);
            GameEntry.TimerComponent.AddOnceTimer(300,()=> { Close(); procedureMain.GotoMenu(); });
        }

        public void GetGoldUpdate(int arg ) 
        {
            goldCount += arg;
            GoldText.text = GameEntry.Localization.GetString("Controller.GoldText", goldCount);

            GameEntry.ArchiveData.Data.Gold += arg;
        }

        public void GetKillUpdate(int arg)
        {
            killCount += arg;      
            KillCountText.text = GameEntry.Localization.GetString("Controller.KillCountText", killCount);
            GameEntry.ArchiveData.Data.KillCount += arg;
        }
        public void GetEXPUpdate(int arg)
        {
            expCount += arg;
            EXPText.text = GameEntry.Localization.GetString("Controller.EXPText", expCount);
            GameEntry.ArchiveData.Data.player.CurrentEXP += arg;
        }
    }
}